Give an object named animations - which picture, which frames, how long each lasts, how it plays - then start one with animation_play("walk"). A frame can play a sound, send a named event, and carry attack, body and push boxes.
An object can own a list of animations. Open the object in the Code tab (or in the Room Editor) and press Animations... next to its Sprite. Each animation has:
_, starting with a letter) - the word your code uses;hero_attack;Animations are saved with the object. Every copy of the object has them, and two objects that use the same art can have different ones. An object also plays the animations of its parent; if the child has one with the same name, the child's wins.
An object that has no animations works exactly as before: sprite, frame and frame_speed are still yours.
If the picture was drawn in the Paint tab with tags (named stretches of frames) and its own frame times, press Bring in from the picture and every tag becomes an animation with its frames and times. A picture with no tags gives one animation of all its frames.
animation_play("walk");Playing the animation that is already playing does nothing, so it is safe to call every step. animation_play("attack", true) starts it over. animation_current says which is playing, animation_frame which frame, and animation_finished whether a play-once animation is done. The Animation End event also says which animation ended (animation_ended_name).
Names are checked when the game is built: a slip like "wlak" is an error that says you probably meant "walk".
Mark a frame with a sound and it plays when the frame starts, with no code. Mark it with an event name and the object's Animation Event runs there, where animation_event_name is the name.
Each frame can hold up to four boxes of each kind. A box is where its top-left corner is from the object's own place (x, y), and its width and height, all in pixels. They flip with the picture (a negative xscale, or flip_x, mirrors them left to right) and do not grow with xscale.
attack_touching asks the same question as a plain yes or no.A hero has three animations:
| Name | Plays | Frames |
|---|---|---|
idle | loop | frame 0, 20 sixtieths |
walk | loop | frames 1, 2, 3, 2; 6 sixtieths each; frame 1 and 3 play snd_step (a footstep, no code) |
attack | play once, then idle | frame 4 for 4, frame 5 for 3 (with an attack box of 14 x 8 starting 6 pixels in front, event name swing), frame 6 for 5 |
In the Step event:
if (btn_pressed(BTN_A)) {
animation_play("attack");
} else if (animation_current() != "attack") {
if (hsp != 0) animation_play("walk"); else animation_play("idle");
}In an Animation Event:
if (animation_event_name == "swing") {
sound_play(snd_swing);
}And Attack hits obj_enemy:
instance_destroy(other);Each swing hurts each enemy once, however long the box overlaps it.
A game with no animations is built exactly as before. A game with animations adds a few bytes to every object and a small table for each animation; the push step and the Attack hits memory are added only when a push box or an Attack hits event exists.